{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Total Distance Traveled"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: distanceTraveled"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #总行驶距离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>卡车有两个油箱。给你两个整数，<code>mainTank</code> 表示主油箱中的燃料（以升为单位），<code>additionalTank</code> 表示副油箱中的燃料（以升为单位）。</p>\n",
    "\n",
    "<p>该卡车每耗费 <code>1</code> 升燃料都可以行驶 <code>10</code> km。每当主油箱使用了 <code>5</code> 升燃料时，如果副油箱至少有 <code>1</code> 升燃料，则会将 <code>1</code> 升燃料从副油箱转移到主油箱。</p>\n",
    "\n",
    "<p>返回卡车可以行驶的最大距离。</p>\n",
    "\n",
    "<p>注意：从副油箱向主油箱注入燃料不是连续行为。这一事件会在每消耗 <code>5</code> 升燃料时突然且立即发生。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mainTank = 5, additionalTank = 10\n",
    "<strong>输出：</strong>60\n",
    "<strong>解释：</strong>\n",
    "在用掉 5 升燃料后，主油箱中燃料还剩下 (5 - 5 + 1) = 1 升，行驶距离为 50km 。\n",
    "在用掉剩下的 1 升燃料后，没有新的燃料注入到主油箱中，主油箱变为空。\n",
    "总行驶距离为 60km 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mainTank = 1, additionalTank = 2\n",
    "<strong>输出：</strong>10\n",
    "<strong>解释：</strong>\n",
    "在用掉 1 升燃料后，主油箱变为空。\n",
    "总行驶距离为 10km 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= mainTank, additionalTank &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [total-distance-traveled](https://leetcode.cn/problems/total-distance-traveled/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [total-distance-traveled](https://leetcode.cn/problems/total-distance-traveled/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5\\n10', '1\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            res += 50\n",
    "            if additionalTank > 0:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return res + mainTank * 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "      ans = 0\n",
    "      while mainTank >= 5:\n",
    "        mainTank -= 5\n",
    "        ans += 50\n",
    "        if additionalTank:\n",
    "          additionalTank -= 1\n",
    "          mainTank += 1\n",
    "      return ans + mainTank * 10 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res=0\n",
    "        while mainTank>=5:\n",
    "            mainTank-=5\n",
    "            res+=50\n",
    "            if additionalTank>0:\n",
    "                additionalTank-=1\n",
    "                mainTank+=1\n",
    "        return res+mainTank*10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, addi: int) -> int:\n",
    "        ans=0\n",
    "        count=0\n",
    "        while mainTank:\n",
    "            ans+=10\n",
    "            count+=1\n",
    "            mainTank-=1\n",
    "            if count==5 and addi:\n",
    "                count=0\n",
    "                mainTank+=1\n",
    "                addi-=1\n",
    "                \n",
    "        \n",
    "        \n",
    "        \n",
    "        return ans\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans=0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 5\n",
    "            if additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        ans+=mainTank\n",
    "        return ans*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        num = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            num += 50\n",
    "            if additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1  \n",
    "        num += mainTank * 10 \n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            c = mainTank//5\n",
    "            ans += c * 50\n",
    "            mainTank %= 5\n",
    "            add = min(c, additionalTank)\n",
    "            additionalTank -= add\n",
    "            mainTank += add\n",
    "        return ans + mainTank*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        mile = 0\n",
    "        while(mainTank>=5):\n",
    "            mainTank -=5\n",
    "            mile += 50\n",
    "            if additionalTank>0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        mile += mainTank*10\n",
    "        return mile\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        distance = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            distance += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return distance + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        t = 0\n",
    "        while True:\n",
    "            div = mainTank // 5\n",
    "            if div == 0:\n",
    "                break\n",
    "            t += div * 5\n",
    "            mod = mainTank % 5\n",
    "            trans = min(div, additionalTank)\n",
    "            additionalTank -= trans\n",
    "            mainTank = mod + trans\n",
    "        t += mainTank\n",
    "        return t * 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            miles5 = mainTank // 5\n",
    "            mainTank -= miles5 * 5\n",
    "            mainTank += min(miles5, additionalTank)\n",
    "            additionalTank -= min(miles5, additionalTank)\n",
    "            ans += miles5 * 5 * 10\n",
    "        return ans + 10 * mainTank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ranshao=0\n",
    "        while mainTank>=5:\n",
    "            mainTank-=5\n",
    "            ranshao+=5\n",
    "            if additionalTank>0:\n",
    "                mainTank+=1\n",
    "                additionalTank-=1\n",
    "        ranshao+=mainTank\n",
    "        return ranshao*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        cnt = 0\n",
    "        while mainTank >= 5 and additionalTank > 0:\n",
    "                mainTank -= 4\n",
    "                additionalTank -= 1\n",
    "                cnt += 5\n",
    "        return (cnt + mainTank) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank + min(additionalTank, (mainTank - 1) // 4)) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        \n",
    "        for i in range(1, 200):\n",
    "            if mainTank >= i and i % 5 == 0 and additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "            \n",
    "            \n",
    "        return mainTank * 10\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "             mainTank -= 5\n",
    "             ans += 50\n",
    "             if additionalTank:\n",
    "                 additionalTank -= 1\n",
    "                 mainTank += 1\n",
    "        return   ans + mainTank * 10\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans=0\n",
    "\n",
    "        while mainTank>=5:\n",
    "            mainTank=mainTank-5\n",
    "            ans+=50\n",
    "            if additionalTank>0:\n",
    "                mainTank+=1\n",
    "                additionalTank-=1\n",
    "        ans+=mainTank*10\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        if mainTank < 5:\n",
    "            return mainTank * 10\n",
    "        res = 0\n",
    "        flag = 0\n",
    "        while mainTank > 0:\n",
    "            flag += 1\n",
    "            res += 10\n",
    "            if flag % 5 == 0 and additionalTank > 0:\n",
    "                additionalTank -= 1\n",
    "            else:\n",
    "                mainTank -= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        s = 0\n",
    "        while mainTank >= 5:\n",
    "            s += 50\n",
    "            mainTank -= 5\n",
    "            if additionalTank >= 1:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        return s + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        km = 0\n",
    "        n = 0\n",
    "        while mainTank > 0:\n",
    "            km, mainTank = km + 10, mainTank - 1\n",
    "            n = n + 1\n",
    "            if n== 5 and additionalTank > 0:\n",
    "                n, mainTank, additionalTank = 0,mainTank + 1, additionalTank - 1\n",
    "        return km"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans=0\n",
    "        while mainTank >=5:\n",
    "               mainTank-=5\n",
    "               ans+=50\n",
    "               if additionalTank:\n",
    "                  additionalTank-=1\n",
    "                  mainTank+=1\n",
    "        return ans + mainTank*10          \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        total=mainTank\n",
    "        while additionalTank and mainTank>=5:\n",
    "            mainTank-=4\n",
    "            additionalTank-=1\n",
    "            total+=1\n",
    "        return total*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank + min(additionalTank, (mainTank - 1) // 4)) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank + min((mainTank - 1) // 4, additionalTank)) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        time = 0\n",
    "        while mainTank >= 5 and additionalTank > 0:\n",
    "            mainTank -= 4\n",
    "            additionalTank -= 1\n",
    "            time += 1\n",
    "        return 10*(5*time + mainTank)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank*10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res = 0\n",
    "        while mainTank >= 5:\n",
    "            res += 5 * 10\n",
    "            mainTank -= 5\n",
    "            if additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "            else:\n",
    "                return res + mainTank * 10\n",
    "        return res + mainTank * 10\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        a=0\n",
    "        while True:\n",
    "            a+=1\n",
    "            mainTank-=1\n",
    "            if a%5==0:\n",
    "                if additionalTank>=1:\n",
    "                    additionalTank-=1\n",
    "                    mainTank+=1\n",
    "            if mainTank==0:    \n",
    "                return a*10\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        km = 0\n",
    "        n = 0\n",
    "        while mainTank > 0:\n",
    "            km, mainTank = km + 10, mainTank - 1\n",
    "            n = n + 1\n",
    "            if n== 5 and additionalTank > 0:\n",
    "                n, mainTank, additionalTank = 0,mainTank + 1, additionalTank - 1\n",
    "        return km"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50 \n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        km = 0\n",
    "        n = 0\n",
    "        while mainTank > 0:\n",
    "            km, mainTank = km + 10, mainTank - 1\n",
    "            n = n + 1\n",
    "            if n== 5 and additionalTank > 0:\n",
    "                n, mainTank, additionalTank = 0,mainTank + 1, additionalTank - 1\n",
    "        return km"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        km = 0\n",
    "        while mainTank > 0:\n",
    "            if mainTank < 5:\n",
    "                return mainTank * 10 + km\n",
    "\n",
    "            count = mainTank // 5      # 这个代表主油箱有几个5的倍数   1\n",
    "            km += count * 10 * 5\n",
    "\n",
    "            if additionalTank >= count:\n",
    "                mainTank = mainTank % 5 + count\n",
    "                additionalTank -= count\n",
    "            else:\n",
    "                mainTank = mainTank % 5 + additionalTank\n",
    "                additionalTank = 0\n",
    "\n",
    "        return km\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        oils = 0\n",
    "        while mainTank >= 5:\n",
    "            oils += 5\n",
    "            mainTank -= 5\n",
    "            if additionalTank >= 1:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        oils += mainTank\n",
    "        return oils * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        s = 0\n",
    "        c = 0\n",
    "        while mainTank > 0:\n",
    "            s += 10\n",
    "            mainTank -= 1\n",
    "            c += 1\n",
    "            if c == 5 and additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "                c = 0\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        dist = 0\n",
    "        while mainTank :\n",
    "            a, b = divmod(mainTank, 5)\n",
    "            if a:\n",
    "                dist += a * 50\n",
    "            else:\n",
    "                dist += b * 10\n",
    "                break\n",
    "          \n",
    "            t = min(additionalTank, a)\n",
    "            mainTank = t + b\n",
    "            additionalTank -= t\n",
    "    \n",
    "        return dist\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res = 0\n",
    "        while mainTank>=5 and additionalTank>0:\n",
    "            tem = min(mainTank//5,additionalTank)\n",
    "            res +=  tem*50\n",
    "            mainTank -= tem*5\n",
    "            mainTank += tem\n",
    "            additionalTank -= tem\n",
    "        res += mainTank*10\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        result = 0\n",
    "        addition = mainTank\n",
    "        while addition >= 5 and additionalTank > 0:           \n",
    "            total = addition // 5\n",
    "            addition = addition % 5 + min([total, additionalTank])\n",
    "            additionalTank -= total\n",
    "            result += total * 5\n",
    "\n",
    "        result += addition\n",
    "        return result * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "      res = 0\n",
    "      while mainTank >= 5:\n",
    "         res += 50\n",
    "         mainTank -=5\n",
    "         if additionalTank > 0:\n",
    "            additionalTank -= 1\n",
    "            mainTank += 1  \n",
    "      return res + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while True:\n",
    "            if mainTank < 5:\n",
    "                ans += mainTank * 10\n",
    "                break\n",
    "            ans += 50\n",
    "            mainTank -= 5\n",
    "            if additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        km = 0\n",
    "        isok = True\n",
    "        while isok:\n",
    "            if mainTank < 5:\n",
    "                km += mainTank\n",
    "                isok = False\n",
    "            else:\n",
    "                if additionalTank != 0:\n",
    "                    mainTank -= 5\n",
    "                    additionalTank -= 1\n",
    "                    mainTank += 1\n",
    "                    km += 5\n",
    "                    isok = True\n",
    "                else:\n",
    "                    mainTank -= 5\n",
    "                    km += 5\n",
    "        return km*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        n = mainTank // 5\n",
    "        m = mainTank % 5\n",
    "        s = 0\n",
    "        while n > 0:\n",
    "            s += (mainTank - m) * 10\n",
    "            if additionalTank >= n:\n",
    "                mainTank = m + n\n",
    "                additionalTank -= n\n",
    "            else:\n",
    "                mainTank = m + additionalTank\n",
    "                additionalTank = 0\n",
    "            n = mainTank // 5\n",
    "            m = mainTank % 5\n",
    "        s += m * 10\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "               additionalTank -= 1\n",
    "               mainTank += 1\n",
    "        return ans + mainTank * 10     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        c, s = 0, 0\n",
    "        while mainTank > 0:\n",
    "            s += 10\n",
    "            mainTank -= 1\n",
    "            c += 1\n",
    "            if c == 5 and additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "                c = 0\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        acc = 0\n",
    "        while mainTank != 0:\n",
    "            ans += 10\n",
    "            acc += 1\n",
    "            mainTank -= 1\n",
    "            if acc == 5 and additionalTank:\n",
    "                acc = 1\n",
    "                ans += 10\n",
    "                additionalTank -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank  + min((mainTank - 1) // 4 , additionalTank)) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank>=5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans+mainTank*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank + min(additionalTank, (mainTank - 1) // 4)) * 10\n",
    "                \n",
    "           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "             mainTank -= 5\n",
    "             ans += 50\n",
    "             if additionalTank:\n",
    "                 additionalTank -= 1\n",
    "                 mainTank += 1\n",
    "        return   ans + mainTank * 10\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res = 0\n",
    "        cnt = 0\n",
    "        while mainTank >= 1:\n",
    "            mainTank -= 1\n",
    "            cnt += 1\n",
    "            res += 10 \n",
    "            if cnt % 5 == 0 and additionalTank >= 1:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        return res\n",
    "'''2739. 总行驶距离\n",
    "卡车有两个油箱。给你两个整数，mainTank 表示主油箱中的燃料（以升为单位），additionalTank 表示副油箱中的燃料（以升为单位）。\n",
    "该卡车每耗费 1 升燃料都可以行驶 10 km。每当主油箱使用了 5 升燃料时，如果副油箱至少有 1 升燃料，则会将 1 升燃料从副油箱转移到主油箱。\n",
    "返回卡车可以行驶的最大距离。\n",
    "注意：从副油箱向主油箱注入燃料不是连续行为。这一事件会在每消耗 5 升燃料时突然且立即发生。\n",
    "示例 1：\n",
    "输入：mainTank = 5, additionalTank = 10\n",
    "输出：60\n",
    "解释：\n",
    "在用掉 5 升燃料后，主油箱中燃料还剩下 (5 - 5 + 1) = 1 升，行驶距离为 50km 。\n",
    "在用掉剩下的 1 升燃料后，没有新的燃料注入到主油箱中，主油箱变为空。\n",
    "总行驶距离为 60km 。\n",
    "示例 2：\n",
    "输入：mainTank = 1, additionalTank = 2\n",
    "输出：10\n",
    "解释：\n",
    "在用掉 1 升燃料后，主油箱变为空。\n",
    "总行驶距离为 10km 。\n",
    "提示：\n",
    "1 <= mainTank, additionalTank <= 100\n",
    "通过次数\n",
    "6.7K\n",
    "提交次数\n",
    "12.7K\n",
    "通过率\n",
    "53.0%'''\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        oils = 0\n",
    "        while mainTank >= 5:\n",
    "            oils += 5\n",
    "            mainTank -= 5\n",
    "            if additionalTank >= 1:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        oils += mainTank\n",
    "        return oils * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        totalDistance = 0\n",
    "        \n",
    "        # 每次行驶的距离\n",
    "        distancePerFuel = 10\n",
    "        \n",
    "        # 主油箱每耗费 5 升燃料时，如果副油箱至少有 1 升燃料，则会将 1 升燃料从副油箱转移到主油箱\n",
    "        while mainTank > 0:\n",
    "            totalDistance += distancePerFuel\n",
    "            mainTank -= 1\n",
    "            \n",
    "            # 每耗费 5 升燃料时，检查副油箱是否有足够的燃料\n",
    "            if totalDistance % 50 == 0 and additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        \n",
    "        return totalDistance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ret = 0\n",
    "        while True:\n",
    "            sh,mainTank = divmod(mainTank,5)\n",
    "            if sh!=0:\n",
    "                ret += sh*50\n",
    "                if additionalTank>0:\n",
    "                    ex = min(additionalTank,sh)\n",
    "                    additionalTank -= ex\n",
    "                    mainTank += ex\n",
    "            else:\n",
    "                ret += mainTank*10\n",
    "                return ret\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        total = 0\n",
    "        count=0\n",
    "        while mainTank>0:\n",
    "            mainTank-=1\n",
    "            total+=10\n",
    "            count+=1\n",
    "            if count%5==0 and additionalTank>=1:\n",
    "                mainTank+=1\n",
    "                additionalTank-=1\n",
    "        return total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        s = 0\n",
    "        while mainTank >= 5:\n",
    "            s += 50\n",
    "            mainTank -= 5\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "                \n",
    "\n",
    "        return s + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        juli=mainTank*10\n",
    "        n=0\n",
    "        while mainTank//5>=1:\n",
    "            mainTank=mainTank-5+1\n",
    "            additionalTank-=1\n",
    "            n=n+1\n",
    "            if additionalTank==0:\n",
    "                break\n",
    "        juli=juli+n*10\n",
    "        return juli\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "#         # a=mainTank%5\n",
    "#         total=0\n",
    "#         while mainTank>=5:\n",
    "#             mainTank-=5\n",
    "#             total+=50\n",
    "#             if additionalTank:\n",
    "#                 additionalTank-=1\n",
    "#                 mainTank+=1\n",
    "#         print(mainTank)\n",
    "#         distance=total+mainTank*10\n",
    "#         print(distance)\n",
    "#         return distance\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        total=0\n",
    "        while mainTank>=5:\n",
    "            t=mainTank//5\n",
    "            total+=t*50\n",
    "            mainTank=mainTank%5\n",
    "            # t是算起初的主油箱注了几次 5 升，每消耗 5L 副油箱注入 1 升，但这个轮次不能超过副油箱的油体积。\n",
    "            # 比如主油箱 20 升，能消耗 4 个 5L，但副油箱只有 2L，只能补充 2 次\n",
    "            t=min(t,additionalTank)\n",
    "            additionalTank-=t\n",
    "            mainTank+=t\n",
    "        print(mainTank)\n",
    "        distance=total+mainTank*10\n",
    "        return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "\t\tans = 0\n",
    "\t\tif mainTank < 5:\n",
    "\t\t\treturn mainTank*10\n",
    "\t\t#ans += mainTank*10\n",
    "\t\twhile mainTank>=5:\n",
    "\t\t\t\n",
    "\t\t\tmainTank -=5\n",
    "\t\t\tans += 5*10\n",
    "\t\t\t\n",
    "\t\t\tif additionalTank>0:\n",
    "\t\t\t\tmainTank += 1\n",
    "\t\t\t\tadditionalTank -= 1\n",
    "\t\tans += mainTank*10\n",
    "\t\treturn ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        distance = 0\n",
    "        tankCount = 0\n",
    "        while mainTank > 0:\n",
    "            tankCount += 1\n",
    "            mainTank -= 1\n",
    "            distance += 10\n",
    "            if tankCount == 5 and additionalTank > 0:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "                tankCount = 0\n",
    "        return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        distance = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            distance += 50\n",
    "            if additionalTank:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "        return distance + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        while mainTank >= 5:\n",
    "            mainTank -= 5\n",
    "            ans += 50\n",
    "            if additionalTank:\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans + mainTank * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ret = 0\n",
    "        mile = 0\n",
    "        while mainTank:\n",
    "            ret += 10\n",
    "            mile += 1\n",
    "            if mile%5 == 0 and additionalTank:\n",
    "                mainTank += 1\n",
    "                additionalTank -= 1\n",
    "            mainTank -= 1\n",
    "            \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        dist = 0\n",
    "        while mainTank != 0:\n",
    "            if mainTank >= 5 and additionalTank != 0:\n",
    "                dist += 50\n",
    "                mainTank -= 5\n",
    "                additionalTank -=1\n",
    "                mainTank += 1\n",
    "            else:\n",
    "                dist += mainTank * 10\n",
    "                mainTank = 0\n",
    "        return dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        a=(mainTank-1)//4\n",
    "        return (mainTank+min(a,additionalTank))*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (mainTank + min((mainTank - 1) // 4, additionalTank)) * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        res=0\n",
    "        while mainTank >=5:\n",
    "            mainTank -=5\n",
    "            res +=50\n",
    "            if additionalTank:\n",
    "                 additionalTank -=1\n",
    "                 mainTank +=1\n",
    "        return res + mainTank*10         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        return (min((mainTank - 1) // 4, additionalTank) + mainTank)*10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        use = 0\n",
    "        while mainTank >=5:\n",
    "            if additionalTank > 0:\n",
    "                mainTank = mainTank - 5 + 1\n",
    "                additionalTank -= 1\n",
    "            else:\n",
    "                mainTank = mainTank - 5\n",
    "            use += 5\n",
    "        use += mainTank\n",
    "        return 10 * use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        ans = 0\n",
    "        cost = 0\n",
    "        while mainTank:\n",
    "            mainTank -= 1\n",
    "            cost += 1\n",
    "            ans += 10\n",
    "            if cost == 5 and additionalTank:\n",
    "                cost = 0\n",
    "                additionalTank -= 1\n",
    "                mainTank += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
    "        #  输入：mainTank = 5, additionalTank = 10\n",
    "        # 输出：60\n",
    "        # 解释：\n",
    "        # 在用掉 5 升燃料后，主油箱中燃料还剩下 (5 - 5 + 1) = 1 升，行驶距离为 50km 。\n",
    "        # 在用掉剩下的 1 升燃料后，没有新的燃料注入到主油箱中，主油箱变为空。\n",
    "        # 总行驶距离为 60km 。\n",
    "        ans = 10 * mainTank\n",
    "        while True:\n",
    "            add_times = mainTank // 5\n",
    "            if not add_times: break\n",
    "            mainTank -= add_times * 5\n",
    "            add = min(additionalTank, add_times)\n",
    "            ans += 10 * add\n",
    "            additionalTank -= add\n",
    "            mainTank += add\n",
    "\n",
    "        return ans\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
